package tech.yunland.shop.domain.model;

import com.alibaba.fastjson.annotation.JSONField;
import com.moyuer.bsf.domain.model.User;
import org.apache.commons.lang3.StringUtils;
import org.hibernate.annotations.NotFound;
import org.hibernate.annotations.NotFoundAction;
import org.hibernate.validator.constraints.Length;
import org.hibernate.validator.constraints.Range;

import javax.persistence.*;
import java.math.BigDecimal;
import java.util.Date;
import java.util.HashSet;
import java.util.Set;

/**
 * 优惠活动
 *
 * @author: 袁首京<yuanshoujing@gmail.com>
 * 16/4/13
 */
@Entity
@Table(name = "goods_package")
public class GoodsPackage extends BaseEntity {

    @Column(length = 30)
    private String code; // 代码

    @Length(min = 1, max = 100)
    @Column(length = 100, nullable = false)
    private String name; // 名称

    @Column(length = 100, nullable = false)
    private String spell; // 拼写

    @JSONField(serialize = false)
    @OneToMany(mappedBy = "goodsPackage")
    private Set<GoodsPackagePictures> pictures; // 插图

    @OneToMany(mappedBy = "owner")
    private Set<GoodsPackageEntry> entries; // 分录

    @Column(nullable = false)
    private BigDecimal price = BigDecimal.ZERO; // 销售单价

    @Column(nullable = false)
    private BigDecimal prize = BigDecimal.ZERO; // 提成金额

    @Column(nullable = false)
    private Long rate = 0l; // 权重

    @Range(min = -1, max = 1)
    @Column(nullable = false)
    private Integer state = 0; // 状态 -1 删除 0 未上架 1 上架

    @JSONField(serialize = false)
    @ManyToOne
    @NotFound(action = NotFoundAction.IGNORE)
    private User creator; // 创建人

    @JSONField(format = "yyyy-MM-dd HH:mm:ss")
    @Column(name = "create_time", nullable = false, insertable = false, updatable = false)
    private Date createTime;

    @Transient
    private String creatorId;
    @Transient
    private String creatorName;

    @Transient
    private String pictureIds;
    @Transient
    private String pictureNames;

    public String getCode() {
        return code;
    }

    public void setCode(String code) {
        this.code = code;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String getSpell() {
        return spell;
    }

    public void setSpell(String spell) {
        this.spell = spell;
    }

    public Set<GoodsPackagePictures> getPictures() {
        return pictures;
    }

    public void setPictures(Set<GoodsPackagePictures> pictures) {
        this.pictures = pictures;
    }

    public BigDecimal getPrice() {
        return price;
    }

    public void setPrice(BigDecimal price) {
        if (price == null) {
            this.price = BigDecimal.ZERO;
        }
        else {
            this.price = price;
        }
    }

    public BigDecimal getPrize() {
        return prize;
    }

    public void setPrize(BigDecimal prize) {
        if (prize == null) {
            this.prize = BigDecimal.ZERO;
        }
        else {
            this.prize = prize;
        }
    }

    public Long getRate() {
        return rate;
    }

    public void setRate(Long rate) {
        this.rate = rate;
    }

    public Set<GoodsPackageEntry> getEntries() {
        return entries;
    }

    public void setEntries(Set<GoodsPackageEntry> entries) {
        this.entries = entries;
    }

    public Integer getState() {
        return state;
    }

    public void setState(Integer state) {
        this.state = state;
    }

    public User getCreator() {
        return creator;
    }

    public void setCreator(User creator) {
        this.creator = creator;
    }

    public Date getCreateTime() {
        return createTime;
    }

    public void setCreateTime(Date createTime) {
        this.createTime = createTime;
    }

    public String getCreatorId() {
        if (creatorId != null) {
            return creatorId;
        }

        if (creator != null) {
            creatorId = creator.getId();
        }

        return creatorId;
    }

    public void setCreatorId(String creatorId) {
        this.creatorId = creatorId;
    }

    public String getCreatorName() {
        if (creatorName != null) {
            return creatorName;
        }

        if (creator != null) {
            creatorName = creator.getName();
        }

        return creatorName;
    }

    public void setCreatorName(String creatorName) {
        this.creatorName = creatorName;
    }

    public String getPictureIds() {
        if (pictureIds != null) {
            return pictureIds;
        }

        if (pictures != null) {
            Set<String> idSet = new HashSet<String>();
            for (GoodsPackagePictures goodsPackagePictures : pictures) {
                idSet.add(goodsPackagePictures.getPicture().getId());
            }

            pictureIds = StringUtils.join(idSet, ", ");
        }

        return pictureIds;
    }

    public void setPictureIds(String pictureIds) {
        this.pictureIds = pictureIds;
    }

    public String getPictureNames() {
        if (pictureNames != null) {
            return pictureNames;
        }

        if (pictures != null) {
            Set<String> nameSet = new HashSet<String>();
            for (GoodsPackagePictures goodsPackagePictures : pictures) {
                nameSet.add(goodsPackagePictures.getPicture().getFileName());
            }

            pictureNames = StringUtils.join(nameSet, ", ");
        }
        
        return pictureNames;
    }

    public void setPictureNames(String pictureNames) {
        this.pictureNames = pictureNames;
    }
}
